﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.Collections.Generic;

public partial class Controls_Search_UserSearch : System.Web.UI.UserControl
{
    private int m_PageSize;
    private int m_Count;
    private List<User> users;

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSearch.Click += new EventHandler(btnSearch_Click);
        ddl_Province.SelectedIndexChanged += new EventHandler(ddl_Province_SelectedIndexChanged);
        cb_City.CheckedChanged += new EventHandler(cb_City_CheckedChanged);
        Pager.PageChanged += new EventHandler(Pager_PageChanged);
        Pager.NumericButtonTextFormatString = "<span style='font-size:14px;'>{0}</span>";
        if (!IsPostBack)
        {
            InitDDLProvince();
            InitDDLCity();
        }
        BindPager();
    }

    void Pager_PageChanged(object sender, EventArgs e)
    {
        BindGridView();
    }

    void cb_City_CheckedChanged(object sender, EventArgs e)
    {
        if (cb_City.Checked == true)
        {
            ddl_Province.Enabled = true;
            ddl_City.Enabled = true;
        }
        else
        {
            ddl_Province.Enabled = false;
            ddl_City.Enabled = false;
        }
    }

    void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitDDLCity();
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        string fullname = txtFullName.Text.Trim();
        if (cb_City.Checked == true)
        {
            Response.Redirect("~/home/search.aspx" + "?cityid=" + Convert.ToInt32(ddl_City.SelectedValue) + "&username=" + Server.HtmlEncode(fullname));
        }
        else
        {
            Response.Redirect("~/home/search.aspx" + "?username=" + Server.HtmlEncode(fullname));
        }
    }

    protected void imgDataBindHandle(object sender, EventArgs e)
    {
        Image img = sender as Image;
        img.ImageUrl = ApiHelper.Instance.GetUserLogoUrl(img.ImageUrl);
    }

    protected void hyDataBindHandle(object sender, EventArgs e)
    {
        HyperLink hy = sender as HyperLink;
        hy.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(hy.NavigateUrl);
    }

    protected void hyDataBindHandle_Image(object sender, EventArgs e)
    {
        HyperLink hy = sender as HyperLink;
        hy.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(hy.NavigateUrl);
    }


    private void InitDDLProvince()
    {
        ddl_Province.DataSource = UserRules.Instance.GetProvinces();
        ddl_Province.DataTextField = "ProvinceName";
        ddl_Province.DataValueField = "ProvinceId";
        ddl_Province.DataBind();
    }
    private void InitDDLCity()
    {
        ddl_City.DataSource = UserRules.Instance.GetCitiesByProvinceId(Convert.ToInt32(ddl_Province.SelectedValue.ToString()));
        ddl_City.DataTextField = "CityName";
        ddl_City.DataValueField = "CityId";
        ddl_City.DataBind();
    }

    private void BindPager()
    {
        if (string.IsNullOrEmpty(Request.QueryString["cityid"]))
        {
            if (Request.QueryString["username"] != null)
            {
                string fullname = Server.HtmlDecode(Request.QueryString["username"]);
                m_Count = FriendRules.Instance.GetSearchFriendsByNameCount(fullname);
                if (m_Count == 0)
                {
                    pn_searchline.Visible = true;
                    pn_NoResult.Visible = true;
                }
                else
                {
                    this.Pager.RecordCount = m_Count;
                    m_PageSize = this.Pager.PageSize;
                    pn_NoResult.Visible = false;
                    pn_searchline.Visible = true;
                }
            }
        }
        else
        {
            if (Request.QueryString["username"] != null)
            {
                string fullname = Server.HtmlDecode(Request.QueryString["username"]);
                int cityid = Convert.ToInt32(Request.QueryString["cityid"]);
                m_Count = FriendRules.Instance.GetSearchFriendsByNameAndCityIdCount(fullname, cityid);
                if (m_Count == 0)
                {
                    pn_searchline.Visible = true;
                    pn_NoResult.Visible = true;
                }
                else
                {
                    this.Pager.RecordCount = m_Count;
                    m_PageSize = this.Pager.PageSize;
                    pn_NoResult.Visible = false;
                    pn_searchline.Visible = true;
                }
            }
        }
    }

    private void BindGridView()
    {
        string fullname = Server.HtmlDecode(Request.QueryString["username"]);
        if (string.IsNullOrEmpty(Request.QueryString["cityid"]))
        {
            if (Request.QueryString["username"] != null)
            {
                users = FriendRules.Instance.SearchFriendsByName(fullname, (this.Pager.CurrentPageIndex - 1) * m_PageSize, m_PageSize);
                this.userSearchList.DataSource = users;
                this.userSearchList.DataBind();
                this.up_Result.Update();
            }
        }
        else
        {
            if (Request.QueryString["username"] != null)
            {
                int cityid = Convert.ToInt32(Request.QueryString["cityid"]);
                users = FriendRules.Instance.SearchFriendsByNameAndCityId(fullname, cityid, (this.Pager.CurrentPageIndex - 1) * m_PageSize, m_PageSize);
                this.userSearchList.DataSource = users;
                this.userSearchList.DataBind();
                this.up_Result.Update();
            }
        }
    }
}
